Imports System.Data.OleDb
Imports System.Collections.ArrayList
Imports System.Collections.Comparer

Public Class RetailPricing
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
    Protected WithEvents txtCustomer As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtMargin As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox3 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox4 As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnGo As System.Web.UI.WebControls.Button
    Protected WithEvents Textbox19 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox18 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox6 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox7 As System.Web.UI.WebControls.TextBox
    Protected WithEvents OrderQty As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox8 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox9 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox20 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox21 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox10 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox11 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox12 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox13 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox14 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox15 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox16 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox17 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox22 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox29 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox30 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox31 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox32 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox33 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox34 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox35 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Textbox36 As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnRecalculated As System.Web.UI.WebControls.Button
    Protected WithEvents hidMeasUnit As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hidRoadWeight As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hid20C As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hid40C As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents hid40HC As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents cboPOOrder As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Textbox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtContainerType As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkSelect As System.Web.UI.WebControls.CheckBox
    Protected WithEvents dlRetailPricingList As System.Web.UI.WebControls.DataList
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents txtMarginForGroup As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblSorting As System.Web.UI.WebControls.Label
    Protected WithEvents hidSortBy As System.Web.UI.HtmlControls.HtmlInputHidden

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private m_hstKeysMap As Hashtable

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        AddHandler dlRetailPricingList.ItemDataBound, AddressOf dlRetailPricingList_ItemDataBound

        If (Not Page.IsPostBack) Then
            Try
                txtCustomer.Text = CType(Session(Security.CURRENT_USER_SESSION_NAME), String)
                LoadRetailPricing()
                LoadData()
            Catch ex As Exception
            End Try
        End If

    End Sub

    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        MyBase.OnPreRender(e)
        Me.ViewState("VS_KEYSMAP") = m_hstKeysMap
    End Sub

#Region " Constant Prefix "
    Private Const CHK_PREFIX As String = "Chk_{0}"
    Private Const ID_PREFIX As String = "ID_{0}"
    Private Const OrderQty_PREFIX As String = "OrderQty_{0}"
    Private Const ProdCode_PREFIX As String = "ProdCode_{0}"
    Private Const DESC_PREFIX As String = "Desc_{0}"
    Private Const SellUnit_PREFIX As String = "SellUnit_{0}"
    Private Const DiaLen_PREFIX As String = "DiaLen_{0}"
    Private Const Width_PREFIX As String = "Width_{0}"
    Private Const Height_PREFIX As String = "Height_{0}"
    Private Const Capacity_PREFIX As String = "Capacity_{0}"
    Private Const CostPrice_PREFIX As String = "Cost_{0}"
    Private Const RetailPrice_PREFIX As String = "Retail_{0}"
    Private Const Margin_PREFIX As String = "Margin_{0}"
    Private Const MarginPert_PREFIX As String = "MarginPert_{0}"
    Private Const Makeup_PREFIX As String = "Makeup_{0}"
    Private Const TotalCost_PREFIX As String = "TotalCost_{0}"
    Private Const TotalSelling_PREFIX As String = "TotalSelling_{0}"
    Private Const TotalMargin_PREFIX As String = "TotalMargin_{0}"
    Private Const BarCode_PREFIX As String = "BarCode_{0}"
#End Region
  
    Private Sub dlRetailPricingList_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        If (e.Item.ItemType = ListItemType.Header) Then
        End If
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim oRetailPricingItem As RetailPricingItem
            oRetailPricingItem = DirectCast(e.Item.DataItem, RetailPricingItem)

            If (m_hstKeysMap Is Nothing) Then
                m_hstKeysMap = New Hashtable
            End If

            Dim chkSelect As CheckBox = CType(e.Item.FindControl("chkSelect"), CheckBox)
            chkSelect.ID = String.Format(CHK_PREFIX, oRetailPricingItem.RL_Id.ToString)
            chkSelect.Checked = oRetailPricingItem.Checked
            m_hstKeysMap.Add(chkSelect.ID, chkSelect.UniqueID)

            Dim txtItem As TextBox = CType(e.Item.FindControl("txtItem"), TextBox)
            txtItem.ID = String.Format(ID_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtItem.Text = oRetailPricingItem.RL_Id.ToString
            m_hstKeysMap.Add(txtItem.ID, txtItem.UniqueID)

            Dim txtOrderQty As TextBox = CType(e.Item.FindControl("txtOrderQty"), TextBox)
            txtOrderQty.ID = String.Format(OrderQty_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtOrderQty.Text = oRetailPricingItem.RL_Quantity.ToString
            m_hstKeysMap.Add(txtOrderQty.ID, txtOrderQty.UniqueID)

            Dim txtProductCode As System.Web.UI.WebControls.TextBox
            txtProductCode = CType(e.Item.FindControl("txtProductCode"), System.Web.UI.WebControls.TextBox)
            txtProductCode.ID = String.Format(ProdCode_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtProductCode.Text = oRetailPricingItem.RL_Product_Code
            m_hstKeysMap.Add(txtProductCode.ID, txtProductCode.UniqueID)

            Dim txtDesc As System.Web.UI.WebControls.TextBox
            txtDesc = CType(e.Item.FindControl("txtDesc"), System.Web.UI.WebControls.TextBox)
            txtDesc.ID = String.Format(DESC_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtDesc.Text = oRetailPricingItem.RL_Description
            m_hstKeysMap.Add(txtDesc.ID, txtDesc.UniqueID)

            Dim txtSellUnit As System.Web.UI.WebControls.TextBox
            txtSellUnit = CType(e.Item.FindControl("txtSellUnit"), System.Web.UI.WebControls.TextBox)
            txtSellUnit.ID = String.Format(SellUnit_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtSellUnit.Text = CType(IIf(oRetailPricingItem.RL_Sell_Unit, "piece", "set"), String)
            m_hstKeysMap.Add(txtSellUnit.ID, txtSellUnit.UniqueID)

            Dim txtDia As System.Web.UI.WebControls.TextBox
            txtDia = CType(e.Item.FindControl("txtDia"), System.Web.UI.WebControls.TextBox)
            txtDia.ID = String.Format(DiaLen_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtDia.Text = oRetailPricingItem.RL_Length.ToString(Config.DecimalFormat2)
            m_hstKeysMap.Add(txtDia.ID, txtDia.UniqueID)

            Dim txtWidth As System.Web.UI.WebControls.TextBox
            txtWidth = CType(e.Item.FindControl("txtWidth"), System.Web.UI.WebControls.TextBox)
            txtWidth.ID = String.Format(Width_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtWidth.Text = oRetailPricingItem.RL_Width.ToString(Config.DecimalFormat2)
            m_hstKeysMap.Add(txtWidth.ID, txtWidth.UniqueID)

            Dim txtHeight As System.Web.UI.WebControls.TextBox
            txtHeight = CType(e.Item.FindControl("txtHeight"), System.Web.UI.WebControls.TextBox)
            txtHeight.ID = String.Format(Height_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtHeight.Text = oRetailPricingItem.RL_Height.ToString(Config.DecimalFormat2)
            m_hstKeysMap.Add(txtHeight.ID, txtHeight.UniqueID)

            Dim txtCapacity As System.Web.UI.WebControls.TextBox
            txtCapacity = CType(e.Item.FindControl("txtCapacity"), System.Web.UI.WebControls.TextBox)
            txtCapacity.ID = String.Format(Capacity_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtCapacity.Text = (oRetailPricingItem.Capacity).ToString(Config.DecimalFormat2)
            m_hstKeysMap.Add(txtCapacity.ID, txtCapacity.UniqueID)

            Dim txtCostPrice As System.Web.UI.WebControls.TextBox
            txtCostPrice = CType(e.Item.FindControl("txtCostPrice"), System.Web.UI.WebControls.TextBox)
            txtCostPrice.ID = String.Format(CostPrice_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtCostPrice.Text = oRetailPricingItem.RL_Price.ToString(Config.DecimalFormat2)
            m_hstKeysMap.Add(txtCostPrice.ID, txtCostPrice.UniqueID)

            Dim txtRetailPrice As System.Web.UI.WebControls.TextBox
            txtRetailPrice = CType(e.Item.FindControl("txtRetailPrice"), System.Web.UI.WebControls.TextBox)
            txtRetailPrice.ID = String.Format(RetailPrice_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtRetailPrice.Text = oRetailPricingItem.RL_Retail.ToString(Config.DecimalFormat2)
            m_hstKeysMap.Add(txtRetailPrice.ID, txtRetailPrice.UniqueID)

            Dim txtMargin As System.Web.UI.WebControls.TextBox
            txtMargin = CType(e.Item.FindControl("txtMargin"), System.Web.UI.WebControls.TextBox)
            txtMargin.ID = String.Format(Margin_PREFIX, oRetailPricingItem.RL_Id.ToString)
            If (Not (oRetailPricingItem.RL_Margin = 0D Or oRetailPricingItem.RL_Margin = Decimal.MinValue)) Then
                txtMargin.Text = oRetailPricingItem.RL_Margin.ToString(Config.DecimalFormat2)
            End If
            m_hstKeysMap.Add(txtMargin.ID, txtMargin.UniqueID)

            Dim txtMarginPercent As System.Web.UI.WebControls.TextBox
            txtMarginPercent = CType(e.Item.FindControl("txtMarginPercent"), System.Web.UI.WebControls.TextBox)
            txtMarginPercent.ID = String.Format(MarginPert_PREFIX, oRetailPricingItem.RL_Id.ToString)
            m_hstKeysMap.Add(txtMarginPercent.ID, txtMarginPercent.UniqueID)

            Dim txtMarkup As System.Web.UI.WebControls.TextBox
            txtMarkup = CType(e.Item.FindControl("txtMarkup"), System.Web.UI.WebControls.TextBox)
            txtMarkup.ID = String.Format(Makeup_PREFIX, oRetailPricingItem.RL_Id.ToString)
            m_hstKeysMap.Add(txtMarkup.ID, txtMarkup.UniqueID)

            Dim txtTotalCost As System.Web.UI.WebControls.TextBox
            txtTotalCost = CType(e.Item.FindControl("txtTotalCost"), System.Web.UI.WebControls.TextBox)
            txtTotalCost.ID = String.Format(TotalCost_PREFIX, oRetailPricingItem.RL_Id.ToString)
            m_hstKeysMap.Add(txtTotalCost.ID, txtTotalCost.UniqueID)

            Dim txtTotalSelling As System.Web.UI.WebControls.TextBox
            txtTotalSelling = CType(e.Item.FindControl("txtTotalSelling"), System.Web.UI.WebControls.TextBox)
            txtTotalSelling.ID = String.Format(TotalSelling_PREFIX, oRetailPricingItem.RL_Id.ToString)
            m_hstKeysMap.Add(txtTotalSelling.ID, txtTotalSelling.UniqueID)

            Dim txtTotalMargin As System.Web.UI.WebControls.TextBox
            txtTotalMargin = CType(e.Item.FindControl("txtTotalMargin"), System.Web.UI.WebControls.TextBox)
            txtTotalMargin.ID = String.Format(TotalMargin_PREFIX, oRetailPricingItem.RL_Id.ToString)
            If ((Not (oRetailPricingItem.RL_Margin = 0D Or oRetailPricingItem.RL_Margin = Decimal.MinValue)) AndAlso (Not (oRetailPricingItem.RL_Quantity = 0D Or oRetailPricingItem.RL_Margin = Decimal.MinValue))) Then
                txtTotalMargin.Text = (oRetailPricingItem.RL_Margin * oRetailPricingItem.RL_Quantity).ToString(Config.DecimalFormat2)
            Else
                txtTotalMargin.Text = ""
            End If
            m_hstKeysMap.Add(txtTotalMargin.ID, txtTotalMargin.UniqueID)

            Dim txtBarCode As System.Web.UI.WebControls.TextBox
            txtBarCode = CType(e.Item.FindControl("txtBarCode"), System.Web.UI.WebControls.TextBox)
            txtBarCode.ID = String.Format(BarCode_PREFIX, oRetailPricingItem.RL_Id.ToString)
            txtBarCode.Text = oRetailPricingItem.RL_Barcode.ToString
            m_hstKeysMap.Add(txtBarCode.ID, txtBarCode.UniqueID)

        End If
        If (e.Item.ItemType = ListItemType.Footer) Then
            Dim total As Integer = 0
            For Each item As RetailPricingItem In RetailPricingList
                total += item.RL_Quantity
            Next
            Dim txtTotalQty As System.Web.UI.WebControls.TextBox
            txtTotalQty = CType(e.Item.FindControl("txtTotalQty"), System.Web.UI.WebControls.TextBox)
            txtTotalQty.Text = total.ToString
        End If
    End Sub

    Private Sub LoadRetailPricing()
        Try
            Dim connStr As String = "Provider=SQLOLEDB;" & System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
            Dim con As New OleDbConnection(connStr)
            con.Open()
            Dim cmdText As String = "Select * from Retail_Lines "
            Dim cmd As New OleDbCommand(cmdText, con)
            Dim reader As OleDbDataReader = cmd.ExecuteReader()
            Dim i As Integer = 0
            Dim item As New ListItem
            Dim oCollection As New ArrayList
            Dim newItem As New RetailPricingItem
            While (reader.Read())
                newItem = New RetailPricingItem
                newItem.RL_Id = CType(reader("RL_Id"), Integer)
                newItem.RL_Product_Code = CType(reader("RL_Product_Code"), String)
                newItem.RL_Description = CType(reader("RL_Description"), String)
                newItem.RL_Quantity = CType(reader("RL_Quantity"), Integer)
                newItem.RL_Sell_Unit = CType(reader("RL_Sell_Unit"), Boolean)
                newItem.RL_N_Set = CType(reader("RL_N_Set"), Boolean)
                newItem.RL_Length = CType(reader("RL_Length"), Decimal)
                newItem.RL_Width = CType(reader("RL_Width"), Decimal)
                newItem.RL_Height = CType(reader("RL_Height"), Decimal)
                newItem.RL_Volume = CType(reader("RL_Volume"), Decimal)
                newItem.RL_Price = CType(reader("RL_Price"), Decimal)
                newItem.RL_Retail = CType(reader("RL_Retail"), Decimal)
                newItem.RL_Barcode = CType(reader("RL_Barcode"), String)

                newItem.Capacity = newItem.RL_Length * newItem.RL_Width * newItem.RL_Height
                oCollection.Add(newItem)
            End While
            RetailPricingList = oCollection
            BindDataList()

            reader.Close()
            con.Close()
            con = Nothing
            cmd = Nothing
        Catch ex As Exception

        End Try
    End Sub

    Private Property RetailPricingList() As ArrayList
        Get
            Return CType(Session("RetailPricingList"), ArrayList)
        End Get
        Set(ByVal Value As ArrayList)
            Session("RetailPricingList") = Value
        End Set
    End Property

    Private Sub LoadData()
        Try
            Dim cusId As Integer = CType(Session("CR_Customer_Id"), Integer)
            Dim connStr As String = "Provider=SQLOLEDB;" & System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
            Dim con As New OleDbConnection(connStr)
            con.Open()
            Dim cmdText As String = String.Format("Select * from PO_Customer_Lines where CR_Customer_Id='{0}' ", cusId)
            Dim cmd As New OleDbCommand(cmdText, con)
            Dim reader As OleDbDataReader = cmd.ExecuteReader()
            Dim i As Integer = 0
            Dim item As New ListItem
            While (reader.Read())
                item = New ListItem(CType(reader("PONo_Number"), String), Convert.toString(reader("PONo_Type")) & i.ToString())
                cboPOOrder.Items.Insert(i, item)
                i = i + 1
            End While
            If (i > 0) Then
                cboPOOrder.SelectedIndex = 0
                Dim value As String = cboPOOrder.SelectedValue.Substring(0, 1)
                If (value = "W") Then
                    txtContainerType.Text = "Warehouse"
                End If
                If (value = "D") Then
                    txtContainerType.Text = "Direct FOB"
                End If
                If (value = "H") Then
                    txtContainerType.Text = "Direct HD"
                End If

            End If
            con.Close()
            con = Nothing
            cmd = Nothing
        Catch ex As Exception

        End Try

    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim oRequestForm As System.Collections.Specialized.NameValueCollection
        oRequestForm = Request.Form
        m_hstKeysMap = DirectCast(Me.ViewState("VS_KEYSMAP"), Hashtable)
        Dim strKey, strValue As String
        For Each item As RetailPricingItem In RetailPricingList
            strKey = m_hstKeysMap(String.Format(ID_PREFIX, item.RL_Id)).toString
            strValue = oRequestForm(strKey)
        Next

        PopulateDataToListFromInput()
        BindDataList()

    End Sub

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Try
            PopulateDataToListFromInput()

            Dim oRequestForm As System.Collections.Specialized.NameValueCollection
            oRequestForm = Request.Form
            m_hstKeysMap = DirectCast(Me.ViewState("VS_KEYSMAP"), Hashtable)
            Dim strKey, strValue As String
            For Each item As RetailPricingItem In RetailPricingList
                strKey = m_hstKeysMap(String.Format(CHK_PREFIX, item.RL_Id)).toString
                strValue = oRequestForm(strKey)
                item.Checked = False
                If (strValue = "on") Then
                    item.RL_Margin = Convert.ToDecimal(txtMarginForGroup.Text)
                    item.Checked = True
                End If
            Next
            BindDataList()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub PopulateDataToListFromInput()
        Try
            Dim oRequestForm As System.Collections.Specialized.NameValueCollection
            oRequestForm = Request.Form
            m_hstKeysMap = DirectCast(Me.ViewState("VS_KEYSMAP"), Hashtable)
            Dim strKey, strValue As String
            For Each item As RetailPricingItem In RetailPricingList
                ''checkbox and margin column
                'strKey = m_hstKeysMap(String.Format(CHK_PREFIX, item.RL_Id)).toString
                'strValue = oRequestForm(strKey)
                'item.Checked = False
                'If (strValue = "on") Then
                '    item.RL_Margin = Convert.ToDecimal(txtMarginForGroup.Text)
                '    item.Checked = True
                'Else
                '    strKey = m_hstKeysMap(String.Format(Margin_PREFIX, item.RL_Id)).toString
                '    strValue = oRequestForm(strKey)
                '    item.RL_Margin = Convert.ToDecimal(IIf(strValue.Trim.Length > 0, strValue, 0))
                'End If
                'margin column
                strKey = m_hstKeysMap(String.Format(Margin_PREFIX, item.RL_Id)).toString
                strValue = oRequestForm(strKey)
                item.RL_Margin = Convert.ToDecimal(IIf(strValue.Trim.Length > 0, strValue, 0))

                'Item column
                strKey = m_hstKeysMap(String.Format(ID_PREFIX, item.RL_Id)).toString
                strValue = oRequestForm(strKey)
                item.RL_Id = Convert.ToInt32(IIf(strValue.Trim.Length > 0, strValue, 0))
                'OrderQty column
                strKey = m_hstKeysMap(String.Format(OrderQty_PREFIX, item.RL_Id)).toString
                strValue = oRequestForm(strKey)
                item.RL_Quantity = Convert.ToInt32(IIf(strValue.Trim.Length > 0, strValue, 0))
                'ProdCode column
                strKey = m_hstKeysMap(String.Format(ProdCode_PREFIX, item.RL_Id)).toString
                item.RL_Product_Code = oRequestForm(strKey)
                'Description column
                strKey = m_hstKeysMap(String.Format(DESC_PREFIX, item.RL_Id)).toString
                item.RL_Description = oRequestForm(strKey)
                ''Sell_Unit column
                'strKey = m_hstKeysMap(String.Format(SellUnit_PREFIX, item.RL_Id)).toString
                'strValue = oRequestForm(strKey)
                ''item.RL_Sell_Unit

                'CostPrice column
                strKey = m_hstKeysMap(String.Format(CostPrice_PREFIX, item.RL_Id)).toString
                strValue = oRequestForm(strKey)
                item.RL_Price = Convert.ToDecimal(IIf(strValue.Trim.Length > 0, strValue, 0))
                'RetailPrice column
                strKey = m_hstKeysMap(String.Format(RetailPrice_PREFIX, item.RL_Id)).toString
                strValue = oRequestForm(strKey)
                item.RL_Retail = Convert.ToDecimal(IIf(strValue.Trim.Length > 0, strValue, 0))
                'Margin column done above

                ''MarginPert column
                'strKey = m_hstKeysMap(String.Format(MarginPert_PREFIX, item.RL_Id)).toString
                'strValue = oRequestForm(strKey)

                'Makeup_PREFIX column
                'TotalCost_PREFIX
                'TotalSelling_PREFIX
                'TotalMargin_PREFIX
                'BarCode_PREFIX
            Next
        Catch ex As Exception
        End Try
    End Sub

    Private Sub BindDataList()
        m_hstKeysMap = New Hashtable
        If (CurrentSortByField() = "Capacity") Then ' sorted by Capacity, then (Dia/Len), then Height
            RetailPricingList.Sort(New CapacitySortHelper)
        Else
            RetailPricingList.Sort(New SimpleComparer(CurrentSortByField, SortOrderDirection.Descending))
        End If
        dlRetailPricingList.DataSource = RetailPricingList
        dlRetailPricingList.DataBind()
    End Sub

    Public Sub RecalculatedButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        PopulateDataToListFromInput()

        BindDataList()
    End Sub

#Region " Sort Field "
    Private Function CurrentSortByField() As String
        Return Convert.toString(IIf(hidSortBy.Value.Trim.Length > 0, hidSortBy.Value, "RL_Id"))
    End Function
    Public Sub SortItemField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Item"
        hidSortBy.Value = "RL_Id"
        BindDataList()
    End Sub

    Public Sub SortProductCodeField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Product Code"
        hidSortBy.Value = "RL_Product_Code"
        BindDataList()
    End Sub

    Public Sub SortDescriptionField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Description"
        hidSortBy.Value = "RL_Description"
        BindDataList()
    End Sub

    Public Sub SortDiaLenField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Dia(len)"
        hidSortBy.Value = "RL_Length"
        BindDataList()
    End Sub

    Public Sub SortWidthField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Width"
        hidSortBy.Value = "RL_Width"
        BindDataList()
    End Sub

    Public Sub SortHeightField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Height"
        hidSortBy.Value = "RL_Height"
        BindDataList()
    End Sub

    Public Sub SortCapacityField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Capacity"
        hidSortBy.Value = "Capacity"
        BindDataList()
    End Sub

    Public Sub SortCostPriceField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Cost Price"
        hidSortBy.Value = "RL_Price"
        BindDataList()
    End Sub

    Public Sub SortrRetailPriceField(ByVal s As Object, ByVal e As EventArgs)
        lblSorting.Text = "Retail Price"
        hidSortBy.Value = "RL_Retail"
        BindDataList()
    End Sub
#End Region

    Private Class CapacitySortHelper
        Implements IComparer

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
             Implements System.Collections.IComparer.Compare
            Dim xx As RetailPricingItem = CType(x, RetailPricingItem)
            Dim yy As RetailPricingItem = CType(y, RetailPricingItem)

            If (xx.Capacity = yy.Capacity) Then
                'compare Dia(Len) then Height
                If (xx.RL_Length = yy.RL_Length) Then
                    'compare Height
                    If (xx.RL_Height > yy.RL_Height) Then
                        Return 1
                    ElseIf (xx.RL_Height < yy.RL_Height) Then
                        Return -1
                    Else
                        Return 0
                    End If
                Else
                        If (xx.RL_Length > yy.RL_Length) Then
                        Return 1
                        Else
                        Return -1
                        End If
                    End If
                Else
                If (xx.Capacity > yy.Capacity) Then
                    Return 1
                Else
                    Return -1
                End If
            End If
            Return 0
        End Function

    End Class
End Class

